﻿//  Copyright (c) 2010 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using System.Web.UI;
using System.Xml;


namespace DNA.Mvc.jQuery
{
    public class TreeView : DataBindableComponentContainer<TreeNode> 
        //  DataBindableComponentContainer<TreeNode>
    {
        public List<TreeNode> Nodes
        {
            get { return InnerItems.Select(item => { return (TreeNode)item; }).ToList(); }
            set { InnerItems = value; }
        }

        private int expandLevel = -1;

        [jQueryIgnore]
        public int ExpandedLevel
        {
            get { return expandLevel; }
            set { expandLevel = value; }
        }

        public override string TagName
        {
            get
            {
                return "ul";
            }
        }

        private HtmlTemplate clientTemplate = new HtmlTemplate();

        public HtmlTemplate ClientTemplate
        {
            get { return clientTemplate; }
            set { clientTemplate = value; }
        }

        /// <summary>
        /// Gets/Sets the remote url that contains node data
        /// </summary>
        public string RemoteDataSource { get; set; }

        private string httpMethod = "POST";

        public string HttpMethod
        {
            get { return httpMethod; }
            set { httpMethod = value; }
        }

        protected void RenderClientTemplate(HtmlTextWriter writer)
        {
            if (!ClientTemplate.IsEmpty)
            {
                writer.WriteBeginTag("script");
                writer.WriteAttribute("id", Id + "_tmpl");
                writer.WriteAttribute("type", "text/x-jquery-tmpl");
                writer.Write(HtmlTextWriter.TagRightChar);
                ClientTemplate.WriteTo(writer);
                writer.WriteEndTag("script");
            }
        }
        public override void RenderEndTag(HtmlTextWriter writer)
        {
            base.RenderEndTag(writer);
            RenderClientTemplate(writer);
        }
    }
}
